package org.xtown.bingluo.xtieba.db;

import org.xtown.bingluo.xtieba.model.XtiebaAuthor;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class XtiebaAuthorDb {
	// 拿到SQLiteDatabase对象
	private DBHelper dbhelp;
	private String TB_NAME;
	private static XtiebaAuthorDb mXtiebaAuthorDbInstance;
	public static String ID = "_id";
	public static String URL = "url";
	public static String NAME = "name";
	public static String LOCAL_AVATAR = "local_avatar";
	public static String INTERNET_AVATAR = "internet_avatar";
	public static String CREATED_AT = "created_at";
	public static String UPDATED_AT = "updated_at";

	private XtiebaAuthorDb(Context context) {
		this.dbhelp = new DBHelper(context);
		this.TB_NAME = DBInfo.Table.AUTHOR_TB_NAME;
	}
	
	public static XtiebaAuthorDb getNetInstance(Context c) {
        synchronized (XtiebaAuthorDb.class) {
        	XtiebaAuthorDb m = mXtiebaAuthorDbInstance;
            if (m == null) {
                m = new XtiebaAuthorDb(c);
                mXtiebaAuthorDbInstance = m;
            }
            m = mXtiebaAuthorDbInstance;
            return m;
        }
	}

	// 保存
	public XtiebaAuthor save(XtiebaAuthor o) {
		delete(0);
		XtiebaAuthor tmp = selectByName(o.name);
		if (tmp == null) {
			if (o.id == 0) {
				//添加
				o = insert(o);
			} else {
				//更新
				update(o);
			}
		} else {
			o.id = tmp.id;
			//更新
			update(o);
		}
		return o;
	}

	//随后就是增删改查的方法

	public XtiebaAuthor selectByName(String name) {
        SQLiteDatabase db = dbhelp.getReadableDatabase();
        Cursor cur = db.query(TB_NAME, new String[] {"*"}, NAME + " = \"" + name + "\"", null, null, null, null);
        XtiebaAuthor o = null;
        if (cur != null) {
        	while (cur.moveToNext()) {
        		o = makeXtiebaAuthor(cur);
        		break;
    		}
        	cur.close();
        }
        return o;
	}

	private XtiebaAuthor makeXtiebaAuthor(Cursor cur) {
		return new XtiebaAuthor(
			cur.getInt(cur.getColumnIndex(ID)),
			cur.getString(cur.getColumnIndex(URL)),
			cur.getString(cur.getColumnIndex(NAME)),
			cur.getString(cur.getColumnIndex(LOCAL_AVATAR)),
			cur.getString(cur.getColumnIndex(INTERNET_AVATAR)),
			cur.getInt(cur.getColumnIndex(CREATED_AT)),
			cur.getInt(cur.getColumnIndex(UPDATED_AT))
		);
	}

	public XtiebaAuthor insert(XtiebaAuthor o){
        SQLiteDatabase db = dbhelp.getWritableDatabase();
        ContentValues cv = makeContentValues(o);
        int id = (int)db.insert(TB_NAME, null, cv);
        o.id = id;
        return o;
	}

	public int delete(int id){
        SQLiteDatabase db = dbhelp.getWritableDatabase();
        String where = ID + "=?";
        String[] whereValue = {Integer.toString(id)};
        return db.delete(TB_NAME, where, whereValue);
	}

	public int update(XtiebaAuthor o){
        SQLiteDatabase db = dbhelp.getWritableDatabase();
        String where = ID + "=?";
        String[] whereValue = {Integer.toString(o.id)};
        ContentValues cv = makeContentValues(o);
        return db.update(TB_NAME, cv, where, whereValue);
	}

	private ContentValues makeContentValues(XtiebaAuthor o) {
		ContentValues cv = new ContentValues();
        cv.put(URL, o.url);
        cv.put(NAME, o.name);
        cv.put(LOCAL_AVATAR, o.local_avatar);
        cv.put(INTERNET_AVATAR, o.internet_avatar);
        cv.put(CREATED_AT, o.created_at);
        cv.put(UPDATED_AT, o.updated_at);
		return cv;
	}
	
	//limit offset,length
	public Cursor getListCursor(int offet, int length){
		SQLiteDatabase dbopen = dbhelp.getReadableDatabase();
		Cursor cursor = dbopen.rawQuery("SELECT * FROM " + DBInfo.Table.AUTHOR_TB_NAME + " LIMIT " + offet + "," + length, null);
		return cursor;
	}
	
	public void deleteAll(){
    	SQLiteDatabase dbopen = dbhelp.getReadableDatabase();  
    	String sql1 = "DELETE FROM " + DBInfo.Table.AUTHOR_TB_NAME;    
    	String sql2 = "SELECT * FROM sqlite_sequence";    
    	String sql3 = "UPDATE sqlite_sequence SET seq=0 WHERE name='" + DBInfo.Table.AUTHOR_TB_NAME + "'";
        dbopen.execSQL(sql1);
        dbopen.rawQuery(sql2, null);
        dbopen.execSQL(sql3);
    }
	
	public void close() {
		this.dbhelp.close();
	}
}